#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/signalfd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/signalfd.h>
#include <sys/types.h>
#include <sys/time.h>

#define SEND_INTERVAL 1
static int32_t ON_MSG = SIGRTMIN + 7;

static long get_ms_time() {
    struct timeval tv;
    memset(&tv, 0x00, sizeof(tv));

    gettimeofday(&tv, nullptr);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

int send_signal(int dest_pid, int signo) {
    static pid_t pid = getpid();
    
    sigval value;
    memset(&value, 0x00, sizeof(sigval));
    value.sival_int = pid;

    int ret = sigqueue(dest_pid, signo, value);
    if (ret != 0)
        perror("sigqueue error");

    return ret;
}

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage:%s dest_pid\n", *argv);
        return -1;
    }

    int msg_total = 100000;
    long send_start = get_ms_time();

    for (int i = 0; i< msg_total; ++i) {
        send_signal(atoi(argv[1]), ON_MSG);
    }

    double send_freq = (1000.0 * msg_total) / (get_ms_time() - send_start);

    printf("msg total:%d send_freq:%lf\n", msg_total, send_freq);

    return 0;
}
